package network.chat.socket;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;

public class IHMClient extends JFrame implements WindowListener {

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		try {
			client.write("/quit" + "<" + client.getId() + ">");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	private Client client;

	private final JButton sendButton;
	private final JTextArea textMessage;
	private final JTextArea textScreen;

	private final static String TEXT_SEND_BUTTON = "Send";
	private final static int DIMENSION_TEXT_SCREEN_WIDTH = 400;
	private final static int DIMENSION_TEXT_SCREEN_HEIGHT = 400;
	private final static int DIMENSION_TEXT_MESSAGE_HEIGHT = 50;

	public IHMClient() {
		// TODO Auto-generated constructor stub
		sendButton = new JButton(TEXT_SEND_BUTTON);
		sendButton.setForeground(Color.BLACK);
		sendButton.setBackground(Color.WHITE);
		sendButton.setPreferredSize(new Dimension(
				sendButton.getMinimumSize().width,
				DIMENSION_TEXT_MESSAGE_HEIGHT));
		textMessage = new JTextArea();
		textScreen = new JTextArea();

		addComponents();
		manageEvents();

		setLocationRelativeTo(null);
		pack();
		setVisible(true);
	}

	public void setClient(Client client) {
		this.client = client;
		setTitle(client.getId());
	}

	private void addComponents() {
		JPanel panelScreen = new JPanel();
		panelScreen.setBackground(Color.BLACK);
		textScreen.setPreferredSize(new Dimension(DIMENSION_TEXT_SCREEN_WIDTH,
				DIMENSION_TEXT_SCREEN_HEIGHT));
		panelScreen.add(textScreen);
		getContentPane().add(panelScreen, BorderLayout.CENTER);
		JPanel panelSouth = new JPanel();
		panelSouth.setBackground(Color.BLACK);
		int dimWidth = DIMENSION_TEXT_SCREEN_WIDTH
				- sendButton.getPreferredSize().width;
		textMessage.setPreferredSize(new Dimension(dimWidth,
				DIMENSION_TEXT_MESSAGE_HEIGHT));
		panelSouth.add(textMessage);
		panelSouth.add(sendButton);
		getContentPane().add(panelSouth, BorderLayout.SOUTH);
	}

	private void manageEvents() {
		manageButtonEvents();
		manageFrameEvents();
	}
	
	private void manageFrameEvents() {
//		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	private void manageButtonEvents() {
		sendButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String message = textMessage.getText();
				if (!(message.endsWith("\n"))) {// || !(message.endsWith("\r")))) {
					message += "\r";
				}
				final String messageToSend = message;
				SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() {
					@Override
					protected Object doInBackground() throws Exception {
						try {
							client.write(messageToSend);
						} catch (IOException e2) {
							displayException(e2.getMessage());
						}
						return null;
					}

					@Override
					protected void done() {
						textMessage.setText("");
						textMessage.requestFocus();
					}
				};
				sw.execute();
			}
		});
	}

	public void setTextScreen(String message) {
		textScreen.append(message + "\n");
	}

	public void displayException(String message) {
		new JOptionPane(message);
	}
}
